package com.cqy.spring.cloud.web;

import com.cqy.spring.cloud.feign.TestFeign;
import com.cqy.spring.cloud.vo.Cqy;
import com.cqy.spring.cloud.vo.Demo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.util.List;

@Slf4j
@RequestMapping("/server")
@RestController
public class ServerController {

    @Autowired
    TestFeign testFeign;

    @GetMapping("/test")
    public Object a() {
        Cqy cqy = new Cqy();
        cqy.setAge(1000);
        cqy.setName("caiqiyuan");
        return testFeign.test(cqy);
    }

    /**
     * create demo
     *
     * @param demo
     * @return
     */
    @PostMapping(value = "/demo")
    Demo create(@RequestBody @Valid Demo demo) {
        return testFeign.create(demo);
    }

    /**
     * update demo
     *
     * @param demo
     * @return
     */
    @PutMapping(value = "/demo")
    Demo update(@RequestBody @Valid Demo demo) {
        return testFeign.update(demo);
    }

    /**
     * delete demo
     *
     * @param id
     * @return
     */
    @DeleteMapping(value = "/demo/{id}")
    Demo delete(@PathVariable(name = "id") String id) {
        return testFeign.delete(id);
    }

    /**
     * list demo
     *
     * @param id
     * @param headerValue test header value
     * @return
     */
    @GetMapping(value = "/demo/{id}")
    Demo get(@PathVariable(name = "id") String id, @RequestHeader(name = "header") String headerValue) {
        return testFeign.get(id, headerValue);
    }

    /**
     * list demos
     *
     * @return
     */
    @GetMapping(value = "/demos")
    List<Demo> list() {
        return testFeign.list();
    }

    /**
     * upload file
     *
     * @param file
     * @return
     */
    @PostMapping(value = "/demo/upload")
    String upload(@RequestPart(name = "file") MultipartFile file) {
        return testFeign.upload(file);
    }
}
